﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using Microsoft.Phone.Controls;
using Subsonic.ViewModels;
using MichMan.Utilities.Requests;
using MichMan.Utilities;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Reflection;
using System.Windows.Media;
using System.Collections.Generic;
using System.Collections;

namespace Subsonic.Controls
{
    public class MediaPivot : Pivot
    {
        public MediaPivot()
        {
            this.LoadingPivotItem += new EventHandler<PivotItemEventArgs>(MusicPivot_LoadingPivotItem);
            this.LoadedPivotItem += new EventHandler<PivotItemEventArgs>(MusicPivot_LoadedPivotItem);
        }

        public int ScrollAmount
        {
            get
            {
                Finder<MediaLongListSelector> mllSel = new Finder<MediaLongListSelector>(SelectedPivotItem);
                if (mllSel.Value() != null)
                {
                    return mllSel.Value().ScreenFirstIndex;
                }
                return 0;
            }
            set
            {
                Finder<MediaLongListSelector> llSel = new Finder<MediaLongListSelector>(SelectedPivotItem);

                if (llSel.Value() != null)
                {
                    List<object> items = new List<object>();

                    // Make a flat list of the items.
                    foreach (var group in llSel.Value().ItemsSource)
                    {
                        items.Add(group);
                        IList list = group as IList;
                        foreach (var item in list)
                        {
                            items.Add(item);
                        }
                    }

                    var scrollToItem = items.ElementAtOrDefault(value);
                    if (scrollToItem != null)
                    {
                        llSel.Value().ScrollTo(scrollToItem);
                    }
                }
            }
        }

        private PivotItem SelectedPivotItem
        {
            get
            {
                if (SelectedItem != null)
                {
                    return GetContainer(SelectedItem);
                }
                return null;
            }
        }

        public IRequest LoadPivots(PivotViewModel pvm)
        {
            if (pvm == null)
            {
                return null;
            }

            pvm.Loaded += (s, lea) =>
            {
                if (pvm.Pivots != null && pvm.Pivots.Count != 0)
                {
                    SelectedItem = pvm.Pivots[0];
                }
            };
            IRequest req = pvm.Load();
            App.Current.AttachRequest(req);
            return req;
        }

        private void MusicPivot_LoadedPivotItem(object sender, PivotItemEventArgs e)
        {
            if (e.Item != null && e.Item.DataContext != null && e.Item.DataContext is BasePivotItemViewModel &&
                DataContext != null & DataContext is PivotViewModel)
            {
                PivotViewModel pvm = (PivotViewModel)DataContext;
                this.SelectedIndex = pvm.Pivots.IndexOf((BasePivotItemViewModel)e.Item.DataContext);
            }
        }

        Finder<MediaApplicationPage> _page = new Finder<MediaApplicationPage>();

        private void MusicPivot_LoadingPivotItem(object sender, PivotItemEventArgs eventArgs)
        {
            if (eventArgs.Item != null && eventArgs.Item.DataContext != null && eventArgs.Item.DataContext is BasePivotItemViewModel)
            {
                IRequest req = ((BasePivotItemViewModel)eventArgs.Item.DataContext).Load();
                App.Current.AttachRequest(req);

                if (req != null && _page.Value().State.ContainsKey(MediaApplicationPage.PivotScroll))
                {
                    req.RunRequestCompleted += (s1, e1) =>
                    {
                        CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
                    };
                }

            }
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            CompositionTarget.Rendering -= CompositionTarget_Rendering;
            /*
            // TODO:  The scrollbar isn't loaded yet.  Defere setting the scrollamount until the scrollbar is created.
            if (_page.Value().State.ContainsKey(MediaApplicationPage.PivotScroll))
            {
                ScrollAmount = (int)_page.Value().State["PivotScroll"];
                _page.Value().State.Remove(MediaApplicationPage.PivotScroll);
            }
             */
        }
    }
}
